/**
 * 
 */
package bi.uc.client;

import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.app.Service;
import android.content.DialogInterface;
import android.util.Log;
import android.os.Process;
/**
 * @author Administrator
 *
 */
public class ExitApplication extends Application {
//	private static ExitApplication instance;
	private List<Activity> activityList=new LinkedList<Activity>();
	private Activity context;
	private Service service = null;
	

	// 单例模式中获取唯一的ExitApplication 实例
//	public static ExitApplication getInstance() {
//		if (null == instance) {
//			instance = new ExitApplication();
//		}
//		return instance;
//
//	}
	
	
	public void addService(Service service) {
		this.service = service;
	}
	// 添加Activity 到容器中
	public void addActivity(Activity activity) {
		activityList.add(activity);	
		context = activity;
	}
	
	public void exitBox(final String s) {
		context.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				new AlertDialog.Builder(context)
				.setTitle("信息")
				.setIcon(android.R.drawable.ic_dialog_info)
				.setMessage(s)
				.setCancelable(false)		//不能取消
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,int which) {
								// TODO Auto-generated method
								// stub
								exit();
							}
						}).show();
			}
		});
	}
	
/*
	public void offLine() {
		context.runOnUiThread(new Runnable() {
			public void run() {
				new AlertDialog.Builder(context)
				.setTitle("信息")
				.setIcon(android.R.drawable.ic_dialog_info)
				.setMessage("您与服务器的连接已经断开!")
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,int which) {
								// TODO Auto-generated method
								// stub
								exit();
							}
						}).show();
			}
		});
	}
	
	public void offsiteLogin() {
		context.runOnUiThread(new Runnable() {
			public void run() {
				new AlertDialog.Builder(context)
				.setTitle("信息")
				.setIcon(android.R.drawable.ic_dialog_info)
				.setMessage("您的帐号在其他地方登录,您将被迫下线.对您造成的不便敬请谅解！")
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,int which) {
								// TODO Auto-generated method
								// stub
								exit();
							}
						}).show();
			}
		});
	}
	*/
	// 遍历所有Activity 并finish
	public void exit() {
		if (service!=null) {
			Log.e("over", "stopservice");
		}
		int i = 0;
		for (Activity activity : activityList) {
			Log.e("计数", ++i+"");
			activity.finish();
		}		
//		activityList.clear();	
		Log.e("over", "****");
		Process.killProcess(Process.myPid());
//		Log.e("over", "1111");
//		System.exit(0);
	}
	
}
